package initialize

import (
	"fmt"
	"go.uber.org/zap"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"order_srv/global"
	"order_srv/model"
)

var err error

func InitMysql() {
	user := global.AppConf.MysqlConfig.User
	password := global.AppConf.MysqlConfig.Password
	host := global.AppConf.MysqlConfig.Host
	port := global.AppConf.MysqlConfig.Port
	dbName := global.AppConf.MysqlConfig.DbName
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", user, password, host, port, dbName)
	global.DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		zap.S().Error("数据库连接失败")
	}
	zap.S().Info("数据库连接成功")
	//迁移数据库
	global.DB.AutoMigrate(&model.Order{})
}
